import AppUtil from "./app";
import UserUtil from "./user";
import LogUtil from "./log";

class Channel {
  static getAppName() {
    if (window && window.native && window.native.getAppName) {
      return window.native.getAppName()
    }
    if (window && window.native && window.native.getAppInfo) {
      return window.native.getAppInfo().name
    }
    return AppUtil.isTouTiaoMp() ? "花生优惠券" : AppUtil.isWechatMp() ? "花生省米" : "点点商城"
  }

  static getId(_id) {
    if (window.native) {
      return window.native.getId()
    }
    return _id;
  }

  static getRid(_rid) {
    if (window.native) {
      return window.native.getRid()
    }
    return _rid;
  }

  static getCode(_code) {
    if (window.native) {
      return window.native.getCode()
    }
    return _code
  }

  static setGoodsHistory(type, good) {
    if (window && window.native && window.native.setGoodsHistory) {
      window.native.setGoodsHistory(type, good)
    }
  }

  static removeGoodsHistory(type, itemid) {
    if (window && window.native && window.native.removeGoodsHistory) {
      window.native.removeGoodsHistory(type, itemid)
    }
  }

  static getGoodDetail(id) {
    let goodDetail = uni.getStorageSync("good-cache")
    if (goodDetail.itemid === id) {
      return goodDetail
    } else if (window.native) {
      return JSON.parse(window.native.getGoodDetail(id))
    } else {
      return undefined
    }
  }

  static navigation(option) {
    if (window && window.native && option.native && window.native.navigation) {
      let keys = Object.keys(option.params)
      let params = ""
      keys.forEach((key, index) => {
        let value = option.params[key];
        params += key + "=" + value + (index >= keys.length - 1 ? "" : "&");
      })
      window.native.navigation(option.url + "?" + params)
    } else {
      uni.$u.route(option)
    }
  }

  static getHome() {
    if (window.native) {
      window.native.gotoHome()
    } else {
      uni.switchTab({
        url: '/pages/pages/main'
      });
    }
  }

  static getClipboardData(params) {
    if (window && window.native && window.native.getClipboardData) {
      window.native.getClipboardData(params);
    } else {
      uni.getClipboardData(params)
    }
  }

  static setClipboardData(params) {
    if (window && window.native && window.native.setClipboardData) {
      window.native.setClipboardData(params)
    } else {
      uni.setClipboardData(params)
    }
  }

  static shareSheet(data, def) {
    if (window && window.native && window.native.shareSheet) {
      window.native.shareSheet(data)
    } else {
      def()
    }
  }

  static getCoupon(itemData, def) {
    if (window && window.native && window.native.getCoupon) {
      window.native.getCoupon(itemData)
    } else {
      def()
    }
  }

  static showModal(params) {
    if (window && window.native && window.native.showModal) {
      window.native.showModal(params)
    } else {
      uni.showModal(params)
    }
  }

  static showDialogEasy(params) {
    if (window && window.native && window.native.showDialogEasy) {
      window.native.showDialogEasy(params)
    } else {
      uni.showActionSheet({
        title: params.title,
        alertText: params.content,
        itemList: params.buttons,
        success: (ret) => {
          if (params.success) {
            params.success(ret.tapIndex)
          }
        },
        fail: params.fail
      })
    }
  }

  static showToast(params) {
    if (window && window.native && window.native.showToast) {
      window.native.showToast(params)
    } else {
      uni.showToast(params)
    }
  }

  static getAppInfo() {
    if (window && window.native && window.native.getAppInfo) {
      return window.native.getAppInfo()
    } else {
      return {
        icon: "https://oss.syi1.com/4/logo.jpg",
        name: "省钱购",
        productId: 4,
        color: "#FF0036",
        versionCode: 1,
        versionName: "1.0.0",
        debug: false
      }
    }
  }

  static disableService() {
    if (window && window.native && window.native.disableService) {
      window.native.disableService()
    } else {
      Channel.showToast("请先升级应用后操作")
    }
  }

  static toPrivacy() {
    if (window && window.native && window.native.toPrivacy) {
      window.native.toPrivacy()
    } else {
      Channel.showToast("请先升级应用后操作")
    }
  }

  static exitLogin() {
    UserUtil.exit()
    if (window && window.native && window.native.exitLogin) {
      window.native.exitLogin()
    }
    Channel.getHome()
  }

  static toSearchResult(keyword, api) {
    if (window && window.native && window.native.toSearchResult) {
      window.native.toSearchResult(keyword, api)
    } else {
      uni.$u.route({
        url: 'pages/search-result',
        params: {
          keyword: keyword,
          index: api
        }
      })
    }
  }

  static upgrade(params) {
    if (window && window.native && window.native.upgrade) {
      window.native.upgrade(params)
    } else {
      LogUtil.w("TODO upgrade")
    }
  }

  // 跳转应用市场
  // bundleName:包名，默认当前应用包名
  static toAppGalleryDetail(params) {
    if (window && window.native && window.native.upgrade) {
      window.native.toAppGalleryDetail(params)
    } else {
      LogUtil.w("TODO toAppGalleryDetail")
    }
  }

  static isNative() {
    return window.native
  }
}

export default Channel